version 8.2
capture clear
capture log close
set more off
set mem 1000m
set mat 800


****Use the standardized dataset, and augment it with earlier rounds.

**FIRST, get a list of country-year covered by the standardized dataset:
use "Enterprise surveys_judicial_reforms_standardized dataset.dta", replace

*Description of country year:  
keep country year year_baseline2 reform_dum1_post

*year_baseline2 is the year of the baseline

keep if year!=.
*There was no enterprise surveys for these countries

sort country year
keep if country!=country[_n-1]|(country==country[_n-1]&year!=year[_n-1])
***Mostly 2 waves per country

sort country year
save "List of country years in standardized dataset.dta", replace

****SECOND, we eliminate this list from the panel data built:

use "Enterprise surveys_judicial_reforms_earlier waves.dta"
keep country year reform_dum1 reform_dum_small1 reform_db_dum h7a DJ4g1m va_perwo_ppp_trim1 sueing_condition polity2 dtf foreign_aid2_percap ///
DJ4s1m DJ4s2m DJ4c1m DJ4c2m CI DJ4g1 DJ4s1 DJ4s2 DJ4c1 DJ4c2

sort country year 
merge country year using "List of country years in standardized dataset.dta"

*year_baseline3 is the year of the baseline of the standardized:
egen year_baseline3=mean(year_baseline2), by(countryname)

*A code _merge==3 means that the data was merged and exists in the standardized dataset: eliminate it:
*A code _merge==2 means that the data is in the standardized dataset: eliminate it:
drop if _merge==3|_merge==2
drop _merge
count 

**The rest is new data
*sort country year
*keep if country!=country[_n-1]|(country==country[_n-1]&year!=year[_n-1])

browse countryname year reform_dum1


***we keep only the pre data: so take out if year is more than year_baseline3
drop if year>=year_baseline3|year==.

*sort country year
*keep if country!=country[_n-1]|(country==country[_n-1]&year!=year[_n-1])

****we keep only the last year, ie the period just before the standardized dataset:
egen year_max=max(year), by(countryname)
keep if year==year_max



*browse countryname year reform_dum1

*All of these observations have reform_dum1=0, ie no judicial reform
***Therefore, all these pre years have NO JUDICAL REFORM!
tab reform_dum1
*reform_dum1 is an indicator for a judicial reform in those years: it is equal to zero

drop if year==.

drop reform_dum1 reform_dum1_post year_baseline2 year_baseline3

gen pre=1

**generate an indicator that will be useful for the graphs
gen pre_wave_for_graph=1

*browse va_perwo_ppp_trim1


***now we have a database of country years before the standardized dataset that we can append to the standardized dataset:
save "Enterprise surveys_PRE data.dta", replace

use "Enterprise surveys_judicial_reforms_standardized dataset.dta", replace
append using "Enterprise surveys_PRE data.dta"

keep country year pre post reform_dum1 reform_dum_small1 reform_db_dum h7a DJ4g* va_perwo_ppp_trim1 sueing_condition polity2 dtf foreign_aid2_percap sector ///
reform_sum_comp_post reform_sum_limited_post reform_dum2 reform_dum3 reform_dum4 ///
reform_dum_small2 reform_dum_small3 reform_dum_small4 ///
DJ4s1m DJ4s2m DJ4c1m DJ4c2m CI DJ4g1 DJ4s1 DJ4s2 DJ4c1 DJ4c2 ///
reform_edu_gdp_post reform_edu_gdp_post reform_health_gdp_post reform_trans_gdp_post reform_nrj_gdp_post reform_bank_gdp_post reform_indus_gdp_post reform_trade_gdp_post reform_debt_gdp_post reform_tax_gdp_post reform_confl_gdp_post reform_parlia_gdp_post reform_media_gdp_post reform_elec_gdp_post reform_corrup_gdp_post ///
dataset pre_wave_for_graph
   
   
*sort country year
*keep if country!=country[_n-1]|(country==country[_n-1]&year!=year[_n-1])


*browse va_perwo_ppp_trim1 post pre

*sort country year
*keep if country!=country[_n-1]|(country==country[_n-1]&year!=year[_n-1])


***generalize reform_dum1 (a constant indicator) to the pre-years:
ren reform_dum1 temp
egen reform_dum1=max(temp), by(countryname)
drop temp 

***generalize pre_wave_for_graph (a constant indicator):
ren pre_wave_for_graph temp
egen pre_wave_for_graph=mean(temp), by(countryname)
drop temp 
*browse countryname year pre_wave_for_graph



**generalize pre and post to all data:
replace pre=0 if pre==.
replace post=0 if post==.

gen reform_dum1_pre=reform_dum1*pre
gen reform_dum1_post=reform_dum1*post

*browse country year reform_dum1_pre if pre==1

gen reform_dum_small1_pre_TRY=reform_dum_small1*post
gen reform_db_dum_post_TRY=reform_db_dum*post

*browse country year reform_sum_comp_post

egen reform_sum_comp=max(reform_sum_comp_post), by(countryname)
egen reform_sum_limited=max(reform_sum_limited_post), by(countryname)

replace reform_sum_comp_post=0 if pre==1
replace reform_sum_limited_post=0 if pre==1

gen reform_sum_comp_pre=reform_sum_comp*pre
gen reform_sum_limited_pre=reform_sum_limited*pre

browse country year reform_sum_comp_post reform_sum_comp_pre



browse country year reform_edu_gdp_post

local list_reforms edu health trans nrj bank indus trade debt tax confl parlia media elec corrup 
   

foreach var of local list_reforms{
	egen reform_`var'_gdp=max(reform_`var'_gdp_post), by(countryname)
	replace reform_`var'_gdp_post=0 if pre==1
	gen reform_`var'_gdp_pre=reform_`var'_gdp*pre
	}


local list_outcomes reform_dum2 reform_dum3 reform_dum4 reform_dum_small1 reform_dum_small2 reform_dum_small3 reform_dum_small4 reform_db_dum
foreach var of local list_outcomes{
	ren `var' temp
	egen `var'=max(temp), by(countryname)
	drop temp 
	
	gen `var'_pre=`var'*pre
	gen `var'_post=`var'*post
	}

*	browse reform_dum_small1_pre_TRY reform_dum_small1_pre reform_db_dum_post_TRY reform_db_dum_post



local list_triple_diff DJ4g1m DJ4s1m DJ4s2m DJ4c1m DJ4c2m CI DJ4g1 DJ4s1 DJ4s2 DJ4c1 DJ4c2
foreach var3 of local list_triple_diff{

*regenerate triple diff interaction:

local list_qualifier_reform dum1 sum_comp sum_limited dum2 dum3 dum4 ///
edu_gdp health_gdp trans_gdp nrj_gdp bank_gdp indus_gdp trade_gdp debt_gdp tax_gdp confl_gdp parlia_gdp media_gdp elec_gdp corrup_gdp

foreach var2 of local list_qualifier_reform{

gen reform_`var2'_post_`var3'=reform_`var2'_post*`var3'
gen reform_`var2'_pre_`var3'=reform_`var2'_pre*`var3'
}
}

*sort country year
*keep if country!=country[_n-1]|(country==country[_n-1]&year!=year[_n-1])

*browse country year reform_sum_comp_post reform_sum_comp_pre DJ4g1m reform_sum_comp_pre_DJ4g1m

browse country year dataset
ren dataset temp
egen dataset=mean(temp), by(countryname)
drop temp

*browse va_perwo_ppp_trim1 post pre



save "Enterprise surveys_judicial_reforms_standardized dataset_with pre data.dta", replace

exit


